import { Page } from '@/base/Page';
import { CompanyAccount } from '@/base/model/CompanyAccount';
import { deleteCompany, getCompanyAccountList, putEnable } from '@/base/api/CompanyApi';
import { isEmpty } from '@/base/util/Strings';

class Form {
  name = '';
  email = '';
  phone = '';
  createBegin = '';
  createEnd = '';
  date = [];
}

export class ListBloc {
  isShowPsdModal = false;
  accountId = 0;
  form = new Form();

  paged: Page<CompanyAccount> = new Page<CompanyAccount>([], 0, 1, 15, 0, []);
  loading = false;

  load(pageIndex: number, pageSize: number) {
    if (!isEmpty(this.form.date) && !isEmpty(String(this.form.date[0]))) {
      // @ts-ignore
      this.form.createBegin = this.form.date[0].getTime() || '';
      // @ts-ignore
      this.form.createEnd = this.form.date[1].getTime() + 24 * 60 * 60 * 1000 || '';
    } else {
      this.form.createBegin = '';
      this.form.createEnd = '';
    }
    this.loading = true;
    return getCompanyAccountList({
      pageIndex,
      pageSize,
      name: this.form.name,
      email: this.form.email,
      phone: this.form.phone,
      createBegin: this.form.createBegin,
      createEnd: this.form.createEnd
    }).then((res) => {
      this.paged = res.data;
    }).finally(() => {
      this.loading = false;
    });
  }

  removeItem(dateItem: CompanyAccount) {
    this.loading = true;
    return deleteCompany(dateItem.id)
      .finally(() => {
        this.loading = false;
      });
  }

  reset() {
    this.form = new Form();
    this.paged.pageIndex = 1;
    this.paged.pageSize = 15;
    this.load(this.paged.pageIndex, this.paged.pageSize);
  }

  changePsdModal(id: number) {
    this.isShowPsdModal = true;
    this.accountId = id;
  }

  savePwdModal() {
    this.isShowPsdModal = false;
    this.load(this.paged.pageIndex, this.paged.pageSize);
  }

}
